//
//  NSObject+TP_Utility.m
//  E_Car
//
//  Created by 谭鹏 on 2017/6/10.
//  Copyright © 2017年 谭鹏. All rights reserved.
//

#import "NSObject+TP_Utility.h"
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@implementation NSObject (TP_Utility)
-(void)handleNullStringWith:(NSString *)str{
    
//    if ([NSString isEmpty:str]) {
//        return;
//    }
//    DDLogError(@"rumtime");
    unsigned int methodCount = 0;
    Ivar * ivars = class_copyIvarList([self class], &methodCount);
    
    for (unsigned int i = 0; i < methodCount; i ++){
        Ivar ivar = ivars[i];
//            const char * name = ivar_getName(ivar);
            const char * type = ivar_getTypeEncoding(ivar);
        
        
//        NSLog(@"type: %s \nname: %s",type,name);
        if (strstr(type, "@")) {//判断 类型包含 “@“
            if(!strcmp(type, "@\"NSDictionary\"") || !strcmp(type, "@\"NSMutableDictionary\"")){
                continue;
            }
            
            id value = object_getIvar(self, ivar);
//            NSLog(@"value: %@",value);
            if (!strcmp(type, "@\"NSString\"")){
                
                
                if (!value) {
                    object_setIvar(self, ivar, str);
                }
                        if ([value containsString:@"(null)"]) {
                            object_setIvar(self, ivar, @"");
                        }
                
            }else if(!strcmp(type, "@\"NSArray\"") ||!strcmp(type, "@\"NSMutableArray\"")){
               
                for (NSObject * obj in value) {
                    [obj handleNullStringWith:str];//递归
                }
            }else{
                [value handleNullStringWith:str];//递归
            }
        }
        
    }
    free(ivars);
    
    
    
    
    
    
    
}

- (void)showAnyThingIsRecursive:(BOOL)isRecursive{
    
    //    if ([NSString isEmpty:str]) {
    //        return;
    //    }
    //    DDLogError(@"rumtime");
    unsigned int methodCount = 0;
    Ivar * ivars = class_copyIvarList([self class], &methodCount);
    
    for (unsigned int i = 0; i < methodCount; i ++){
        Ivar ivar = ivars[i];
        const char * name = ivar_getName(ivar);
        const char * type = ivar_getTypeEncoding(ivar);
        
        
        NSLog(@"type: %s \nname: %s",type,name);
        if (strstr(type, "@")) {//判断 类型包含 “@“
            if(!strcmp(type, "@\"NSDictionary\"") || !strcmp(type, "@\"NSMutableDictionary\"")){
                continue;
            }
            
            id value = object_getIvar(self, ivar);
            //            NSLog(@"value: %@",value);
            if (!strcmp(type, "@\"NSString\"")){
                
                

            }else if(!strcmp(type, "@\"NSArray\"") ||!strcmp(type, "@\"NSMutableArray\"")){
                if (isRecursive) {
                    for (NSObject * obj in value) {
                        [obj showAnyThingIsRecursive:isRecursive];//递归
                    }
                }
                
            }else{
                if (isRecursive) {
                    [value showAnyThingIsRecursive:isRecursive];//递归
                }
                
            }
        }
        
    }
    free(ivars);

}

- (void)showAnyViewIsRecursive:(BOOL)isRecursive{
    
 [self showAnyViewIsRecursive:isRecursive numberOfLayer:0];
    
}

- (void)showAnyViewIsRecursive:(BOOL)isRecursive numberOfLayer:(NSUInteger)number{
    
    //    if ([NSString isEmpty:str]) {
    //        return;
    //    }
    //    DDLogError(@"rumtime");
    unsigned int methodCount = 0;
    Ivar * ivars = class_copyIvarList([self class], &methodCount);
    
    for (unsigned int i = 0; i < methodCount; i ++){
        Ivar ivar = ivars[i];
        const char * name = ivar_getName(ivar);
        const char * type = ivar_getTypeEncoding(ivar);
        
        
        
        if (strstr(type, "@")) {//判断 类型包含 “@“
            id value = object_getIvar(self, ivar);
            if ([value isKindOfClass:[UIView class]]) {
                NSMutableString * arrows = [NSMutableString new];
                for (int i = 0; i <number ; i ++) {
                    [arrows appendFormat:@"       "];
                }
                UIView * view = value;
                NSLog(@"%@<--- type: %s name: %s frame:%@",arrows,type,name,NSStringFromCGRect(view.frame));
                if (isRecursive) {
                    for (NSObject * obj in [value subviews]) {
                        [obj showAnyViewIsRecursive:isRecursive numberOfLayer:number+1];//递归
                    }
                }
            }
           
        }
        
    }
    free(ivars);
    
}
@end
